<?php
declare(strict_types=1);

namespace app\api\controller\admin;

use app\common\model\delivery\Customer;
use app\common\model\delivery\SellerCommission;
use app\common\model\delivery\SellerSettlement;
use think\annotation\route\Get;
use think\annotation\route\Group;

#[Group("admin/seller")]
class Seller extends Base
{
    #[Get('store')]
    public function store()
    {
        $page=$this->request->get('page/d',1);
        $offest=($page-1)*15;
        $list=Customer::withJoin(['area','commission'],'left')->where(function ($query){
            $seller_id=$this->request->get('seller_id');
            if(!$seller_id){
                $seller_id=$this->seller->id;
            }
            $keywords=$this->request->get('keywords');
            $query->where('customer.channel_id','=',$this->channel->id);
            $query->where('customer.seller_id','=',$seller_id);
            if($keywords){
                $query->where('customer.name','like',"%{$keywords}%");
            }
        })->limit($offest,15)->order('customer.id desc')->select();
        $settletype=SellerCommission::SETTLETYPE;
        $this->success('',compact('list','settletype'));
    }

    #[Get('shouyi')]
    public function shouyi()
    {
        $seller_id=$this->request->get('seller_id');
        if(!$seller_id){
            $seller_id=$this->seller->id;
        }
        $page=$this->request->get('page');
        $offset=($page-1)*15;
        $where=function($query) use ($seller_id){
            $query->where('seller_id',$seller_id);
            $date=$this->request->get('date');
            if($date!='all'){
                $time=time();
                $start='';
                $end='';
                if($date=='year'){
                    $start=strtotime(date('Y-01-01 00:00:00',$time));
                    $end=$time;
                }
                if($date=='last-month'){
                    $year=intval(date('Y',$time));
                    $month=intval(date('m',$time))-1;
                    if($month==0){
                        $month=12;
                        $year--;
                    }
                    if($month<10){
                        $month='0'.$month;
                    }
                    $start=strtotime(date('Y-m-01 00:00:00',strtotime('-1 month',$time)));
                    $end=strtotime($year.'-'.$month.'-01 00:00:00');
                }
                if($date=='this-month'){
                    $start=strtotime(date('Y-m-01 00:00:00',$time));
                    $end=$time;
                }
                if($date=='this-week'){
                    $xs=strtotime(date('Y-m-d 00:00:00',$time));
                    $week=date('w',$time);
                    if($week==0){
                        $week=7;
                    }
                    $start=$xs-($week-1)*24*3600;
                    $end=$time;
                }
                if($date=='yestoday'){
                    $start=strtotime(date('Y-m-d 00:00:00',$time-24*3600));
                    $end=strtotime(date('Y-m-d 23:59:59',$time-24*3600));
                }
                if($date=='today'){
                    $start=strtotime(date('Y-m-d 00:00:00',$time));
                    $end=$time;
                }
                $query->whereBetween('createtime',[$start,$end]);
            }
        };
        $list=SellerCommission::where($where)->limit($offset,15)->order('id desc')->select();
        $total='';
        $tongji=SellerCommission::where($where)->group('order_type')->field('order_type,sum(money) as money,count(1) as count')->select();
        foreach ($tongji as $k=>$xs){
            $total.=SellerCommission::ORDERTYPE[$xs->order_type].$xs->count.'笔，￥'.$xs->money;
            if($k<count($tongji)-1){
                $total.='，';
            }
        }
        $this->success('',compact('list','total'));
    }

    #[Get('jiesuan')]
    public function jiesuan()
    {
        $seller_id=$this->request->get('seller_id');
        if(!$seller_id){
            $seller_id=$this->seller->id;
        }
        $list=SellerSettlement::where(['seller_id'=>$seller_id])->select();
        $this->success('',$list);
    }
}
